package ru.niir.goodfil.db;

import org.apache.commons.lang.StringUtils;
import ru.niir.goodfil.db.dic.ClientState;
import ru.niir.goodfil.db.dic.DeliveryType;
import ru.niir.goodfil.db.dic.StorageDeliveryState;

import javax.persistence.*;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

@Entity
@Table(name = "`StorageDeliveryStatus`")
public class StorageDeliveryStatus implements Unique, Serializable {
    /**
     * Идентификатор заказа
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "`Id`")
    private long id = -1;

    /**
     * Идентификатор клиента, на которого оформлен заказ
     */
    @Column(name = "`IdOrder`")
    private long idOrder = -1;

    @Column(name = "CurrentDate")
    private Date date;

    @Column(name = "IdDeliveryType")
    private int idDeliveryType;

    @Column(name = "DeliveryState")
    private int deliveryState = -1;

    @Column(name = "Comment")
    private String comment;

    @Column(name = "IsSaved")
    private boolean isSaved;

    @Column(name = "DocumentNum1C")
    private String documentNum1C;

    @Column(name = "ClientStateId")
    private int clientStateId;

    @Column(name = "LoadingDate")
    private Date loadingDate;

    @Column(name = "DriverId", nullable = false)
    private long driverId = -1;

    @Transient
    private Order order;

    @Transient
    private Driver driver;

    public StorageDeliveryStatus() {
    }

    public StorageDeliveryStatus(long idOrder, java.util.Date date, int idDeliveryType, int clientStateId) {
        this.idOrder = idOrder;
        this.date = date;
        this.idDeliveryType = idDeliveryType;
        this.comment = "";
        this.documentNum1C = "";
        this.clientStateId = clientStateId;
    }

    public StorageDeliveryStatus(long idOrder, Date date, int idDeliveryType, int deliveryState, String comment, boolean isSaved, String documentNum1C, int clientStateId) {
        this.idOrder = idOrder;
        this.date = date;
        this.idDeliveryType = idDeliveryType;
        this.deliveryState = deliveryState;
        this.comment = comment;
        this.isSaved = isSaved;
        this.documentNum1C = documentNum1C;
        this.clientStateId = clientStateId;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getIdOrder() {
        return idOrder;
    }

    public void setIdOrder(long idOrder) {
        this.idOrder = idOrder;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public int getIdDeliveryType() {
        return idDeliveryType;
    }

    public void setIdDeliveryType(int deliveryState) {
        this.idDeliveryType = deliveryState;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public boolean isSaved() {
        return isSaved;
    }

    public void setSaved(boolean isSaved) {
        this.isSaved = isSaved;
    }

    public String getDocumentNum1C() {
        return documentNum1C;
    }

    public void setDocumentNum1C(String documentNum1C) {
        this.documentNum1C = documentNum1C;
    }

    public int getDeliveryState() {
        return deliveryState;
    }

    public void setDeliveryState(int deliveryState) {
        this.deliveryState = deliveryState;
    }

    public long getDriverId() {
        return driverId;
    }

    public void setDriverId(long driverId) {
        this.driverId = driverId;
    }

    public String getStorageDeliveryStateAsString() {
        return StorageDeliveryState.asString(deliveryState);
    }

    public int getClientStateId() {
        return clientStateId;
    }

    public void setClientStateId(int clientStateId) {
        this.clientStateId = clientStateId;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        assert order != null;
        assert order.getId() == idOrder;
        this.order = order;
    }

    public Driver getDriver() {
        return driver;
    }

    public void setDriver(Driver driver) {
        this.driver = driver;
    }

    public boolean isDeliveryStateIsCharge(){
        return deliveryState == StorageDeliveryState.IS_CHARGED;
    }

    public boolean isDeliveryStateIsDelivery(){
        return deliveryState == StorageDeliveryState.IS_DEVERY;
    }

    public boolean isDeliveryStateIsNotDelivery(){
        return deliveryState == StorageDeliveryState.NOT_DELIVERY;
    }

    public boolean isLoadingDateIsNull(){
        return loadingDate == null;
    }

    public boolean isDriverSet(){
        return getDriverId() != -1;
    }

    public Date getLoadingDate() {
        return loadingDate;
    }

    public String getDateAsString(){
        if (date == null) {
            return StringUtils.EMPTY;
        }
        return new SimpleDateFormat("dd.MM.yyyy").format(date);
    }

    public String getLoadingDateAsString(){
        if (loadingDate == null) {
            return StringUtils.EMPTY;
        }
        return new SimpleDateFormat("dd.MM.yyyy HH:mm").format(loadingDate);
    }

    public void setLoadingDate(Date loadingDate) {
        this.loadingDate = loadingDate;
    }
}
